#include "mainwindow.h"
MainWindow::MainWindow(QWidget *parent, Qt::WindowFlags flags) : QMainWindow(parent, flags)
{
	setupUi(this);
	currSide=0;
	headPic.load(":/images/pointer02.png");
	headOpp=headPic.mirrored(true, false);
	loadSequence();
	initSession(0);
	warningSound=new QSound("sounds/warning.wav");
	warningSound->setLoops(1);
	
	connect(quitAction, SIGNAL(triggered()), this, SLOT(close()));
	connect(newTimingAction, SIGNAL(triggered()), this, SLOT(reset()));
	connect(changeHeadAction, SIGNAL(triggered()), this, SLOT(changeHead()));
	connect(nextButton, SIGNAL(clicked(bool)), this, SLOT(next()));
	connect(pauseButton, SIGNAL(clicked(bool)), this, SLOT(pause()));
	connect(&proTimer, SIGNAL(timeout()), this, SLOT(reduceTime1()));
	connect(&conTimer, SIGNAL(timeout()), this, SLOT(reduceTime2()));
	connect(time1, SIGNAL(timeChanged(const QTime &)), this, SLOT(timeEvent1(const QTime &)));
	connect(time2, SIGNAL(timeChanged(const QTime &)), this, SLOT(timeEvent2(const QTime &)));
}

bool MainWindow::isTiming()
{
	return proTimer.isActive()||conTimer.isActive();
}

void MainWindow::loadSequence()
{
	QFile conf("sessions.conf");
	bool valid;
	if(!conf.exists()) {
		resetConf();
	}
	valid=conf.open(QIODevice::ReadOnly);

	sessionList.clear();
	QString confLine;
	confLine=QString::fromUtf8(conf.readLine());
	DebateSession session;
	while(!confLine.isEmpty()) {
		session=DebateSession(confLine.simplified().split(","));
		if(session.valid) {
			sessionList.append(session);
		}
		else {
			resetConf();
			loadSequence();
			break;
		}
		confLine=QString::fromUtf8(conf.readLine());
	}
	conf.close();
}

bool MainWindow::resetConf()
{
	return QFile::copy(":/etc/sessions.conf", "sessions.conf");
}

void MainWindow::initSession(int seq)
{
	currSession=seq;
	proTimer.stop();
	conTimer.stop();
	disconnect(pauseButton, SIGNAL(clicked()), this, SLOT(resume()));
	pauseButton->setText("Pause");
	connect(pauseButton, SIGNAL(clicked()), this, SLOT(pause()));
	titleLabel->setText(sessionList.at(seq).name);
	proLabel->setText(getSpeakerName(0, sessionList.at(seq).pro));
	conLabel->setText(getSpeakerName(1, sessionList.at(seq).con));

	if(sessionList.at(seq).type==1) {
		time1->setTime(sessionList.at(seq).t_limit);
		time2->setTime(sessionList.at(seq).t_limit);
	}
	else if(sessionList.at(seq).start==0) {
		time1->setTime(sessionList.at(seq).t_limit);
		time2->setTime(QTime(0,0));
	}
	else {
		time2->setTime(sessionList.at(seq).t_limit);
		time1->setTime(QTime(0,0));
	}
	if(sessionList.at(seq).start==0)
		imageLabel->setPixmap(QPixmap::fromImage(headPic));
	else
		imageLabel->setPixmap(QPixmap::fromImage(headOpp));
	currSide=sessionList.at(seq).start;
	switchButton->setEnabled(true);
	switchButton->setText("Start");
	disconnect(switchButton, SIGNAL(clicked(bool)), this, SLOT(change()));
	connect(switchButton, SIGNAL(clicked(bool)), this, SLOT(start()));
	pauseButton->setEnabled(false);
	nextButton->setEnabled(true);
}

QString MainWindow::getSpeakerName(int side, int place)
{
	QString text;
	if(side==0)
		text=trUtf8("正方");
	else
		text=trUtf8("反方");
	switch(place)
	{
		case 1:
			text.append(trUtf8("一辩"));
			break;
		case 2:
			text.append(trUtf8("二辩"));
			break;
		case 3:
			text.append(trUtf8("三辩"));
			break;
		case 4:
			text.append(trUtf8("四辩"));
			break;
	}
	return text;
}

void MainWindow::reduceTime1()
{
	QTime currTime=time1->time();
	QTime nextTime;
	if(currTime.minute()==0 && currTime.second()==0 && currTime.msec()==0) {
		emit timeUp(0);
		proTimer.stop();
		return;
	}

	if(currTime.msec()==0) {
		nextTime=currTime.addMSecs(9);
		nextTime=nextTime.addSecs(-1);
	}
	else {
		nextTime=currTime.addMSecs(-1);
	}
	time1->setTime(nextTime);
}

void MainWindow::reduceTime2()
{
	QTime currTime=time2->time();
	QTime nextTime;
	if(currTime.minute()==0 && currTime.second()==0 && currTime.msec()==0) {
		emit timeUp(1);
		conTimer.stop();
		return;
	}

	if(currTime.msec()==0) {
		nextTime=currTime.addMSecs(9);
		nextTime=nextTime.addSecs(-1);
	}
	else {
		nextTime=currTime.addMSecs(-1);
	}
	time2->setTime(nextTime);
}

void MainWindow::reset()
{
	initSession(0);
}

void MainWindow::pause()
{
	proTimer.stop();
	conTimer.stop();
	disconnect(pauseButton, SIGNAL(clicked()), this, SLOT(pause()));
	pauseButton->setText("Resume");
	connect(pauseButton, SIGNAL(clicked()), this, SLOT(resume()));
}

void MainWindow::resume()
{
	if(sessionList.at(currSession).timing==1) {
		proTimer.start(100);
		conTimer.start(100);
	}
	else if(currSide==0)
		proTimer.start(100);
	else
		conTimer.start(100);
	disconnect(pauseButton, SIGNAL(clicked()), this, SLOT(resume()));
	pauseButton->setText("Pause");
	connect(pauseButton, SIGNAL(clicked()), this, SLOT(pause()));
}

void MainWindow::start()
{
	if(sessionList.at(currSession).timing==1) {
		proTimer.start(100);
		conTimer.start(100);
	}
	else if(currSide==0)
		proTimer.start(100);
	else
		conTimer.start(100);
	pauseButton->setEnabled(true);
	if(sessionList.at(currSession).type==0 || sessionList.at(currSession).timing==1) {
		switchButton->setEnabled(false);
	}
	else {
		switchButton->setText("Switch");
		disconnect(switchButton, SIGNAL(clicked(bool)), this, SLOT(start()));
		connect(switchButton, SIGNAL(clicked(bool)), this, SLOT(change()));
	}
}

void MainWindow::change()
{
	if(currSide==0) {
		proTimer.stop();
		conTimer.start(100);
		imageLabel->setPixmap(QPixmap::fromImage(headOpp));
	}
	else {
		conTimer.stop();
		proTimer.start(100);
		imageLabel->setPixmap(QPixmap::fromImage(headPic));
	}
	currSide=!currSide;
}

void MainWindow::next()
{
	if(sessionList.at(currSession).type==1 || currSide!=sessionList.at(currSession).start) {
		initSession(currSession+1);
		return;
	}
	disconnect(pauseButton, SIGNAL(clicked()), this, SLOT(resume()));
	pauseButton->setText("Pause");
	connect(pauseButton, SIGNAL(clicked()), this, SLOT(pause()));
	proTimer.stop();
	conTimer.stop();
	if(sessionList.at(currSession).start==1) {
		time1->setTime(sessionList.at(currSession).t_limit);
		time2->setTime(QTime(0,0));
		imageLabel->setPixmap(QPixmap::fromImage(headPic));
	}
	else {
		time2->setTime(sessionList.at(currSession).t_limit);
		time1->setTime(QTime(0,0));
		imageLabel->setPixmap(QPixmap::fromImage(headOpp));
	}
	switchButton->setEnabled(true);
	currSide=!currSide;
	if(currSession==sessionList.size()-1) {
		nextButton->setEnabled(false);
	}
}

void MainWindow::timeEvent1(const QTime &time)
{
	QPalette palette;
	if(time==sessionList.at(currSession).t_limit) {
		palette.setColor(QPalette::Text, QColor(0, 93, 127, 255));
		time1->setPalette(palette);
	}
	if(time.minute()==0 && time.second()==30 && time.msec()==0) {
		palette.setColor(QPalette::Text, Qt::red);
		time1->setPalette(palette);
		warningSound->play();
	}
}

void MainWindow::timeEvent2(const QTime &time)
{
	QPalette palette;
	if(time==sessionList.at(currSession).t_limit) {
		palette.setColor(QPalette::Text, QColor(0, 93, 127, 255));
		time2->setPalette(palette);
	}
	if(time.minute()==0 && time.second()==30 && time.msec()==0) {
		palette.setColor(QPalette::Text, Qt::red);
		time2->setPalette(palette);
		warningSound->play();
	}
}

void MainWindow::changeHead()
{
	HeadDialog dialog(this);
	int result=dialog.exec();
	switch(result) {
		case 1:
			headPic.load(":/images/pointer01.png");
			break;
		case 2:
			headPic.load(":/images/pointer02.png");
			break;
		case 3:
			headPic.load(":/images/pointer03.png");
			break;
		default:
			headPic.load(":/images/pointer02.png");
	}
	headOpp=headPic.mirrored(true, false);
	if(currSide==0)
		imageLabel->setPixmap(QPixmap::fromImage(headPic));
	else
		imageLabel->setPixmap(QPixmap::fromImage(headOpp));
}
